import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class login extends JFrame {
	
	private JPanel usernamePanel;
	private JPanel passwordPanel;
	private JLabel username;
	private JComboBox loginField;
	private JLabel passwordLabel;
	private JPasswordField passwordField;
	String[] options = {"", "Admin", "Guest"};
	final int WINDOW_WIDTH = 300;
	final int WINDOW_HEIGHT = 100;
	boolean adminSelected = false; // turns true when admin choice is selected
	
	public login(){
		
		setTitle("Login"); // sets title
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // sets size
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		buildLoginInfo();
		add(usernamePanel, BorderLayout.NORTH); // adds username panel on top
		add(passwordPanel, BorderLayout.CENTER); // adds password panel on the bottom
		setVisible(true);
	}
	
	private void buildLoginInfo(){
		
		username = new JLabel("Username:"); // creates username label
		loginField = new JComboBox(options);// creates username combo box
		loginField.setPreferredSize(new Dimension(113, 20));
		loginField.addActionListener(new usernameListener());
		passwordLabel = new JLabel("Password:"); // creates password label
		passwordField = new JPasswordField(10); // creates password field
		passwordField.addActionListener(new textListener());
		usernamePanel = new JPanel(); // creates username panel
		usernamePanel.add(username); // adds username label
		usernamePanel.add(loginField); // adds combo box
		passwordPanel = new JPanel(); // creates password panel
		passwordPanel.add(passwordLabel); // adds password label
		passwordPanel.add(passwordField); // adds password field
		
	}
	
	private class usernameListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			
			if(loginField.getSelectedItem().equals("Admin")){ // if user selects admin
				adminSelected = true;
			}
			else if(loginField.getSelectedItem().equals("Guest")){ // if user selects guest
				adminSelected = false;
				 int choice = JOptionPane.showConfirmDialog(null, "Selecting guest removes some privileges. Continue?", "Guest Account", JOptionPane.YES_NO_OPTION);
				 if(choice == 0){ // if user chooses yes
					 setVisible(false); // hide login window
					new initUI("Guest"); // open instance of simulator
				 }
				 else{
					 loginField.setSelectedItem(""); // resets username field to default if user presses no
				 }
			}
			else if (loginField.getSelectedItem().equals("")){
				adminSelected = false;
			}
		}
	}
	
	private class textListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			
			if (adminSelected == true && passwordField.getText().equals("comp354")){
				setVisible(false); // hide login window
				new initUI("Administrator"); // open instance of simulator
			}
			else if (adminSelected == false){
				JOptionPane.showMessageDialog(null, "Please select a username", "Warning", JOptionPane.WARNING_MESSAGE);
			}
			else{
				JOptionPane.showMessageDialog(null, "Incorrect password", "Error", JOptionPane.ERROR_MESSAGE);
			
				/*note: the possibility of the user selecting Guest and entering a password is impossible due to
				        the login screen refreshing once "Guest", then "no" is chosen*/
			}
		}
	}
}
